<?php
/**
 * @version		$Id: controller.php 18838 2010-09-11 02:11:21Z ian $
 * @package		Joomla.Site
 * @subpackage	com_content
 * @copyright	Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */

// No direct access
defined('_JEXEC') or die;

jimport('joomla.application.component.controller');

/**
 * Content Component Controller
 *
 * @package		Joomla.Site
 * @subpackage	com_content
 * @since		1.5
 */
class jetext_2Controller extends JController
{
	protected $default_view = 'texts';
	
	/**
	 * Method to display a view.
	 *
	 * @param	boolean			If true, the view output will be cached
	 * @param	array			An array of safe url parameters and their variable types, for valid values see {@link JFilterInput::clean()}.
	 *
	 * @return	JController		This object to support chaining.
	 * @since	1.5
	 */
	public function display($cachable = false, $urlparams = false)
	{
		$cachable = true;

		// Set the default view name and format from the Request.
		$vName		= JRequest::getWord('view', 'texts');
		JRequest::setVar('view', $vName);

		$user = JFactory::getUser();

		if ($user->get('id') || ($_SERVER['REQUEST_METHOD'] == 'POST' &&
			(($vName = 'category' && JRequest::getVar('layout') != 'blog') || $vName = 'archive' ))) {
			$cachable = false;
		}

		$safeurlparams = array('catid'=>'INT','id'=>'INT','cid'=>'ARRAY','year'=>'INT','month'=>'INT','limit'=>'INT','limitstart'=>'INT',
			'showall'=>'INT','return'=>'BASE64','filter'=>'STRING','filter_order'=>'CMD','filter_order_Dir'=>'CMD','filter-search'=>'STRING','print'=>'BOOLEAN','lang'=>'CMD');

		parent::display($cachable, $safeurlparams);

		return $this;
	}
	
	function download()
	{
		$file = JRequest::getVar('file');
		
		if(!$file) return false;
		
		$path = JPATH_ROOT.DS.'upload'.DS.'texts'.DS;
		$fullPath = $path.$file;
		 
		if ($fd = fopen ($fullPath, "r")) 
		{
		    $fsize = filesize($fullPath);
		    
		    $path_parts = pathinfo($fullPath);
		    
		    $ext = strtolower($path_parts["extension"]);
		    
		    switch ($ext) 
		    {
		        case "pdf":
			        header("Content-type: application/pdf"); // add here more headers for diff. extensions
			        header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
			        break;
		        default;
			        header("Content-type: application/octet-stream");
			        header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
		    }
		    
		    header("Content-length: $fsize");
		    header("Cache-control: private"); //use this to open files directly
		    
		    while(!feof($fd)) {
		        $buffer = fread($fd, 2048);
		        echo $buffer;
		    }
		}
		
		fclose ($fd);
		
		exit;
	}

}
